package com.sengled.cloud.biz.union.mgt.dao.sys;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.sengled.cloud.biz.union.mgt.dao.sys.provider.RoleDaoProvider;
import com.sengled.cloud.biz.union.mgt.dao.sys.provider.ShopDaoProvider;
import com.sengled.cloud.biz.union.mgt.model.po.Shop;
import com.sengled.cloud.biz.union.mgt.model.vo.SelectBoxDataVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopDetailVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopEditVo;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopQueryParamsBean;
import com.sengled.cloud.biz.union.mgt.model.vo.ShopVo;


@Mapper
public interface ShopDao {
	@Update("UPDATE shop SET seal_mark=1 WHERE id=#{id}")
    int deleteByPrimaryKey(@Param("id") Long id);
   
    @Insert("INSERT INTO shop(shop_code,shop_name,shop_tel,shop_nature,establish_date,country_code,country_name,province_code,province_name,city_code,city_name,district_code,district_name,street_code,street_name,detailed_addr,shop_mgr,operator_id,company_id,create_date,seal_mark) " +
            "VALUES(#{shop.shopCode},#{shop.shopName},#{shop.shopTel},#{shop.shopNature},#{shop.establishDate},#{shop.countryCode},#{shop.countryName},#{shop.provinceCode},#{shop.provinceName},#{shop.cityCode},#{shop.cityName},#{shop.districtCode},#{shop.districtName},#{shop.streetCode},#{shop.streetName},#{shop.detailedAddr},#{shop.shopMgr},#{shop.operatorId},#{shop.companyId},#{shop.createDate},0)")
    @Options(useGeneratedKeys = true, keyProperty = "shop.id")
    int insert(@Param("shop") Shop shop);

    int insertSelective(Shop record);

    @Select("SELECT shop_code shopCode,shop_name shopName,shop_tel shopTel,CASE WHEN shop_nature=1 THEN '直营' WHEN shop_nature=2 THEN '加盟' ELSE '其他' END shopNature,DATE_FORMAT(establish_date,'%Y-%m-%d') establishDate,CONCAT(IFNULL(country_name,''),IFNULL(province_name,''),IFNULL(city_name,''),IFNULL(district_name,''),IFNULL(street_name,'')) shopArea,detailed_addr detailedAddr,shop_mgr shopMgr FROM shop s WHERE s.id=#{id} ")
    ShopDetailVo selectByPrimaryKey(@Param("id") Long id);

    @Select("SELECT id id,shop_code shopCode,shop_name shopName,shop_tel shopTel,shop_nature shopNature,DATE_FORMAT(establish_date,'%Y-%m-%d') establishDate,country_code countryCode,country_name countryName,province_code provinceCode,province_name provinceName,city_code cityCode,city_name cityName,district_code districtCode,district_name districtName,street_code streetCode,street_name streetName,detailed_addr detailedAddr,shop_mgr shopMgr FROM shop s WHERE s.id=#{id} ")
    ShopEditVo getShopEditData(@Param("id") Long id);
    
    int updateByPrimaryKeySelective(Shop record);
    
    @Update("UPDATE shop SET shop_name=#{shop.shopName},shop_tel=#{shop.shopTel},shop_nature=#{shop.shopNature},establish_date=#{shop.establishDate},country_code=#{shop.countryCode},country_name=#{shop.countryName}"
    		+ ",province_code=#{shop.provinceCode},province_name=#{shop.provinceName},city_code=#{shop.cityCode},city_name=#{shop.cityName},district_code=#{shop.districtCode},district_name=#{shop.districtName},street_code=#{shop.streetCode},street_name=#{shop.streetName} "
    		+ ",detailed_addr=#{shop.detailedAddr},shop_mgr=#{shop.shopMgr},company_id=#{shop.companyId},operator_id=#{shop.operatorId},modify_date=#{shop.modifyDate} WHERE id=#{shop.id}")
    int updateByPrimaryKey(@Param("shop") Shop shop);
    
    
    //@Select("SELECT id,shop_code shopCode,shop_name shopName,shop_tel shopTel,CASE WHEN shop_nature=1 THEN '直营' WHEN shop_nature=1 THEN '加盟' ELSE '其他' END  shopNature FROM shop WHERE (shop_code = #{qp.shopCode}) AND (shop_name = #{qp.shopName}) AND (shop_nature=#{qp.shopNature}) AND (seal_mark = 0) LIMIT #{qp.start},#{qp.pageSize}")
  
    @SelectProvider(type = ShopDaoProvider.class, method = "queryShopSQL")
    List<ShopVo> listMessageByPage(ShopQueryParamsBean queryParamsBean);
    
    
    @SelectProvider(type = ShopDaoProvider.class, method = "queryShopTotalRecordsSQL")
    Integer queryShopTotalRecords(ShopQueryParamsBean queryParamsBean);
    
    @Select("SELECT s.id id,shop_name name FROM shop s where s.int001 is null and s.seal_mark=0 and s.company_id=(SELECT company_id FROM user_account WHERE id=#{userId}) group by s.id,shop_name order by s.create_date desc ")
    List<SelectBoxDataVo> getShopSelectData(Long userId);
    
    @Select("SELECT s.id id,shop_name name FROM shop s where s.int001 is null and s.seal_mark=0 and s.company_id=(SELECT company_id FROM user_account WHERE id=#{userId}) and s.id in(SELECT sa.shop_id FROM user_acct_shop_acct usa,shop_account sa WHERE usa.user_account_id=#{userId} and usa.shop_account_id=sa.id and sa.seal_mark=0) group by s.id,shop_name order by s.create_date desc")
    List<SelectBoxDataVo> getCurrentUserShopSelectData(Long userId);
    
    @Insert("INSERT INTO shop_account(shop_code,shop_name,account_code,shop_id,create_date,company_id,operator_id,seal_mark,account_status,account_pwd) " +
            "VALUES(#{shop.shopCode},#{shop.shopName},#{shop.shopCode},#{shop.id},#{shop.createDate},#{shop.companyId},#{shop.operatorId},0,2,#{shop.str001})")
    @Options(useGeneratedKeys = true, keyProperty = "shop.id")
    int insertShopAccount(@Param("shop") Shop shop);
    
    
    @Update("UPDATE shop_account SET shop_name=#{shop.shopName},account_code=#{shop.shopCode},operator_id=#{shop.operatorId}"
    		+ ",company_id=#{shop.companyId},modify_date=#{shop.modifyDate} WHERE shop_id=#{shop.id}")
    int updateShopAccount(@Param("shop") Shop shop);
    
    //@Delete("delete from shop_account WHERE shop_id=#{id}")
    @Update("UPDATE shop_account SET seal_mark=1 WHERE shop_id=#{id}")
    int deleteShopAccount(@Param("id") Long id);
    
    @Delete("delete from shop_account_module WHERE shop_account_id in (select id from shop_account WHERE shop_id=#{id})")
    int deleteShopAccountModule(@Param("id") Long id);
    
    @Delete("delete from user_acct_shop_acct WHERE shop_account_id in (select id from shop_account WHERE shop_id=#{id})")
    int deleteUserAcctShopAcct(@Param("id") Long id);
    
    @Select("SELECT id from shop_account where shop_id=#{id}")
    Long getShopAccountIdByShopId(@Param("id") Long id);
    
    @Select("SELECT count(id) from shop_account where account_name=#{accountName}")
    int getShopCountByAccountName(@Param("accountName") String accountName);
    
    @Select("SELECT count(id) from shop where shop_code=#{shopCode} and seal_mark=0")
    int getShopCountByShopCode(@Param("shopCode") String shopCode,@Param("companyId") Long companyId);
}